package cetcbigdata.da.subject_integration_service.hainan.legal_person;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

import static cetcbigdata.da.subject_integration_service.hainan.Util.*;

public class Legal_yijianshi extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {

        //按主题分类
        String[] moduleKeys = {"1","2","3","4","5","6","99"};
        for (String moduleKey : moduleKeys){
            String listUrl = "https://wssp.hainan.gov.cn/wssp3/yjs/oneThingList";
            JSONObject listHeader = new JSONObject();
            listHeader.put("Referer","https://wssp.hainan.gov.cn/hnwt/onceHandle");
            listHeader.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
            JSONObject listParmas = new JSONObject();
            listParmas.put("topicType","2");
            listParmas.put("oneThing","");
            listParmas.put("pageNumber","1");
            listParmas.put("pageSize","99");
            listParmas.put("topicValue",moduleKey);
            String listStrResponse = postFormReq(listUrl,listHeader,listParmas);
            JSONObject listResponse = JSONObject.parseObject(listStrResponse);
            if (null != listResponse.getJSONObject("data") && listResponse.getJSONObject("data").size() > 1){
                for (Object o : listResponse.getJSONObject("data").getJSONArray("datas")){
                    JSONObject oneThing = (JSONObject) o;
                    String sxListUrl = "https://wssp.hainan.gov.cn/wssp3/yjs/sxOneThinginfo";
                    JSONObject sxListHeader = new JSONObject();
                    sxListHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/onceHandle");
                    sxListHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                    JSONObject sxListParmas = new JSONObject();
                    sxListParmas.put("topicValue",moduleKey);
                    sxListParmas.put("topicType","2");
                    sxListParmas.put("oneThing",oneThing.getString("onething"));
                    String sxListStrResponse = postFormReq(sxListUrl,sxListHeader,sxListParmas);
                    JSONObject sxListResponse = JSONObject.parseObject(sxListStrResponse);
                    if (null != sxListResponse.getJSONObject("data") && sxListResponse.getJSONObject("data").size() > 1){
                        for (Object o1 : sxListResponse.getJSONObject("data").getJSONArray("datas")){
                            JSONObject reqData = (JSONObject) o1;
                            String id = reqData.getString("id");
                            String url = "https://wssp.hainan.gov.cn/wssp3/yjs/dbznxq?id=" + id;
                            JSONObject header = new JSONObject();
                            header.put("Referer", "https://wssp.hainan.gov.cn/hnwt/oneThing?id="+id+"&basiccode="+reqData.getString("basiccode"));
                            header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");

                            String strResponse = postFormReq(url,header,new JSONObject());
                            JSONObject response = JSONObject.parseObject(strResponse);

                            //==========获取详情页数据================
                            JSONObject result = new JSONObject();
                            if (response.getInteger("code") == 20000) {
                                JSONObject pageDatas = response.getJSONObject("data").getJSONObject("datas");

                                JSONArray sxImplBasicMaterial = pageDatas.getJSONArray("sxImplBasicMaterial");
                                JSONArray sxList = pageDatas.getJSONArray("sxList");
                                JSONObject sxOnethingInfo = pageDatas.getJSONObject("sxOnethingInfo");
                                JSONArray sxOnethingQuestion = pageDatas.getJSONArray("sxOnethingQuestion");
                                //url
                                String pageUrl = "https://wssp.hainan.gov.cn/hnwt/oneThing?id=" + id + "&basiccode=" + reqData.getString("basiccode") + "&version=2";
                                result.put("地址", pageUrl);//完成
                                //开头
                                result.put("名称", sxOnethingInfo.getString("basicname"));//完成
                                //联办效能
                                result.put("联办效能", getOtherInfoData(sxOnethingInfo));//完成
                                //基本信息
                                result.put("基本信息", getBasicInfoData(sxOnethingInfo));//完成
                                //申报须知
                                result.put("申报须知", sxOnethingInfo.getString("acceptcondition"));//完成
                                //办理流程
                                result.put("办理流程图", getFlowChart(id));//完成
                                //办理材料
                                result.put("办理材料", getBasicMaterData(sxImplBasicMaterial));//完成
                                //收费内容
                                result.put("收费内容", "无");//待确认
                                //办理结果
                                result.put("办理结果", getResultData(sxList));
                                //常见问题
                                result.put("常见问题", getQusetionData(sxOnethingQuestion));//完成
                            }
                            System.out.println(
                                    "一件事集成服务-法人办事-"+getModule(moduleKey) + "\n"
                                    + getRegion(reqData.getString("xzqhname")) + "\n"
                                    + result + "\n"
                            );
                            return processResult(
                                    null,
                                    null,
                                    null,
                                    strResponse,
                                    result,
                                    getRegion(reqData.getString("xzqhname")),
                                    "一件事集成服务-法人办事-"+getModule(moduleKey)
                            );
                        }
                    }
                }
            }
        }

        return null;
    }

    public String getModule(String s){
        switch (s){
            case "1":
                return "商超便利";
            case "2":
                return "住宿餐饮";
            case "3":
                return "文体旅游";
            case "4":
                return "医护保健";
            case "5":
                return "农林渔牧";
            case "6":
                return "变更注销";
            case "99":
                return "其他";
        }
        return null;
    }

    public static void main(String[] args) {
        Legal_yijianshi l = new Legal_yijianshi();
        l.getDataDemo();
    }
}
